using UnityEngine;
using System.Collections;
using System.Collections.Generic; // required for Dictionary<>

public class GUIControllerScript : MonoBehaviour {
	
	private static Dictionary<string, string> Inventory = null;
	
	// Use this for initialization
	void Start () {
		Inventory = new Dictionary<string, string>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI () {
		int left = 10;
		int top = 10;
		int width = 400;
		int height = 20;
		int padding = 5;
		int buttonHeight = height * 2;
		
		if (GUI.Button (new Rect (left, top, width, buttonHeight), "Click to load inventory")) {
			Inventory = DatabaseConnectionExampleScript.LoadInventory();
		}
		
		if (Inventory.Count == 0) { return; }
		
		Debug.Log ("Making labels for " + Inventory.Count + " item(s).");
		
		// we've got some inventory, so display it

		top += buttonHeight + padding;		
		GUI.Label(new Rect(left, top, width, height), "We have the following items:");		

		// iterate through the item collection, creating a label to display each one
		foreach (string itemName in Inventory.Keys) {
			// extract the name and, just for fun, create the appropriate indefinite article
			string itemType = Inventory[itemName];
			string ucItemType = itemType.ToUpper();
			string ch1 = ucItemType.Substring(0, 1);
			string article = "AEIOU".Contains(ch1) ? "An" : "A";
			string inventoryDescr = string.Format("{0} {1} named {2}", article, itemType.ToLower(), itemName);

			top += height + padding;
			GUI.Label(new Rect(left, top, width, height), inventoryDescr);			
		}		
	}
}
